<?php

namespace app\common\common\lib;

use think\Log;

class  CxjSdk {

    protected $appid;
    protected $appsecret;
    protected $url;  //请求接口地址
    protected $action;  //请求方法
    protected $timeout; //最大连接时间s
    protected $asyn;


    public function __construct() {
        $this->appid = '10001';
        $this->appsecret = 'ac080f96c440bcb5b1a33d51cc756d64';
        $this->url = config( 'old_url') . '/Home/Cxj/';
        $this->timeout = 10;
    }

    //设置请求方法
    public function  setAction( $action = ''){
        $this->action =   $action;
    }

    public function setCurlAsyn( $sta = false ){
        $this->asyn = $sta;
    }


    /**
     * 获取结果
     * @param array $param 请求参数
     */
    public function getResult( $param ){
        if( !$this->action ){
            throw new \think\Exception('请求action不存在');
        }
        $url = $this->url . $this->action;
        //整合请求参数
        $timestamp = time();
        $data['appid']     = $this->appid;
        $data['timestamp'] = $timestamp;
        $data['sign']      = $this->getSign( $timestamp );  //获取签名

        $data = array_merge( $data , $param );

        return $this->curlQuery( $url , $data );
    }


    /**
     * 修改积分
     */
    public function updateScore( $data ){
        $url = $this->url . $this->action;

        if( !isset( $data['union_id']) || !isset( $data['type']) ||  ! isset( $data['score'])  ){
            throw new \think\Exception('参数错误');
        }
        $timestamp = time();
        $data['timestamp'] = $timestamp;
        $data['sign'] = $this->getSign( $timestamp );

        return $this->curlQuery( $url , $data );
    }


    /**
     * #######################  用户操作 ##################################
     */

    /**
     * 判断旧平台union_id是否存在
     * @param $union_id  微信用户union_id
     */
    public function checkMemberUnionId( $union_id ){
        $url = $this->url . $this->action;

        if( !isset( $union_id ) ){
            throw new \think\Exception('参数错误');
        }
        $data['union_id'] = $union_id;
        $timestamp = time();
        $data['timestamp'] = $timestamp;
        $data['sign'] = $this->getSign( $timestamp );

        return $this->curlQuery( $url , $data  );
    }


    /**
     * 添加新会员
     * @param $openid h5用户Openid
     */
    public function addMember( $openid ){
        $url = $this->url . $this->action;

        if( !isset( $openid ) ){
            throw new \think\Exception('参数错误');
        }
        $data['open_id'] = $openid;
        $timestamp = time();
        $data['timestamp'] = $timestamp;
        $data['sign'] = $this->getSign( $timestamp );

        return $this->curlQuery( $url , $data );
    }

    /**
     * 生成签名
     */
    private function getSign( $timestamp ){
        return md5(  md5( $this->appid . $this->appsecret . $timestamp) . $timestamp );
    }

    private function curlQuery( $url , $postdata = array() , $isPost = 1  , $header = array () ){


        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置否输出到页面
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout ); //设置连接等待时间
        curl_setopt($ch, CURLOPT_HEADER, 0);   //返回头部信息
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        if( $isPost == 1){
            curl_setopt($ch, CURLOPT_POST, $isPost);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        }
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

        if( $this->asyn == true ){
            curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
        }

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
        $data = curl_exec($ch);
        curl_close($ch);


        aw_log('CxjApi:' . json_encode($data) . ' url:' . $url . '    input:' . json_encode($postdata)   , 'cxhsdk.log');
        return json_decode($data , true) ;
    }
}  